package com.xquant.pricing.systerm.service.task;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.*;

/**
 * @author B946
 * @date 2021/1/22 14:12
 * @description:
 */
public class JobScheduledPools {

    private static boolean isRun = false;

    private static final Long DELAY = 3000L;

    private static final Long PERIOD = 3000L;

    private static BlockingQueue<TaskEntity> queue = new LinkedBlockingDeque();

    public static ExecutorService exec = Executors.newSingleThreadScheduledExecutor();

    private static ScheduledExecutorService timer = new ScheduledThreadPoolExecutor(1,
            new BasicThreadFactory.Builder().namingPattern("task-schedule-pool-%d").daemon(true).build());

    public static void addTask(TaskEntity taskEntity) {
        synchronized (queue) {
            //boolean inQueue = queue.add(taskEntity);
            if(!isRun){
                isRun = true;
                run();
            }
        }
    }

    public static void run() {
        JobScheduledTimerTask task = new JobScheduledTimerTask();
        timer.scheduleAtFixedRate(task, DELAY, PERIOD, TimeUnit.MILLISECONDS);
    }

    public synchronized static BlockingQueue<TaskEntity> getPools() {
        return queue;
    }
}
